"""
@File    :   base.py
@Time    :   2024/01/07 20:00:53
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   应用异常基类
"""

from src.enums.http_response_code import HttpResponseEnum, HttpResponseInfo


class AppException(Exception):
    """应用异常基类"""

    def __init__(
        self,
        exc_info: HttpResponseInfo,
        *args,
        code: int | None = None,
        msg: str | None = None,
        err_msg: str = "",
        echo_exc: bool = False,
        **kwargs,
    ):
        super().__init__()
        _code = code if code is not None else exc_info.code
        _message = msg if msg is not None else exc_info.msg
        self._code = _code or HttpResponseEnum.FAILED.value.code
        self._message = _message or HttpResponseEnum.FAILED.value.msg
        self.err_msg = err_msg
        self.echo_exc = echo_exc
        self.args = args or ()
        self.kwargs = kwargs or {}

    @property
    def code(self) -> int:
        return self._code

    @property
    def msg(self) -> str:
        return self._message

    def __str__(self):
        return "{}: {}".format(self.code, self.msg)
